<?php

class Shop_categoryModel extends Model {
	
	var $catelist;
	var $rootlist;
	var $indexlist;
	var $sublist;
	var $html_ext="";
	var $langid = "zh";
	var $all_list;
	var $flist;//格式化后的数组
	
	public function get_primary_key() {
		return $this->primary_key = 'id';
	}


	public function set($id, $data) {
		//$data['site'] = App::get_site_id();
	    if ($id) {
	        $this->update($data, 'id=' . $id);
	        return $id;
	    }
	    $this->insert($data);
	    if ($this->get_insert_id()) return $this->get_insert_id();
	    return false;
	}
	
	public function del($id) {
	    $this->delete('id=' . $id);

	}
	
	public function get_all(){
		$sql = 'select * from '.$this->prefix . 'shop_category'.' where 1=1 order by parentid asc ,id desc';
		$rslist = $this->db->query($sql)->result_array();
		$this->all_list = $rslist;
		return $rslist;
	}	
	
	function get_catelist()
	{
		$sql = "SELECT * FROM ".$this->prefix."shop_category where 1=1";
	
		$sql .= " ORDER BY parentid ASC,id DESC ";
		$rslist = $this->db->query($sql)->result_array();
		if(!$rslist)
		{
			return false;
		}
		$parentlist = array();
		foreach($rslist AS $key=>$value)
		{
			if(!$value["parentid"])
			{
				$this->rootlist[] = $value;
			}
			else
			{
				$this->sublist[] = $value;
				$parentlist[] = $value["parentid"];
			}
		}
		$parentlist = array_unique($parentlist);
		$this->parentlist = $parentlist;
		$this->catelist = $rslist;
		return true;
	}	

	function catelist()
	{
		if(!$this->catelist) return false;
		return $this->catelist;
	}
	
	function format_list($parentid=0,$level=0)
	{
		if(!$this->all_list)
		{
			return false;
		}
		foreach($this->all_list AS $key=>$value)
		{
			if($value["parentid"] == $parentid)
			{
				$value["level"] = $level;
				$this->flist[] = $value;
				$this->format_list($value["id"],($level+1));
			}
		}
		return true;
	}
	
	function flist()
	{
		return $this->flist;
	}
	
	function html_select_array()
	{
		$rslist = array();
		if(!$this->rootlist || !is_array($this->rootlist) || count($this->rootlist)<1)
		{
			return false;
		}
		foreach($this->rootlist AS $key=>$value)
		{
			$value["space"] = $space ? $space : "";
			$value["isend"] = true;
			if($this->parentlist && in_array($value["id"],$this->parentlist))
			{
				$value["isend"] = false;
			}
			$rslist[] = $value;
			$this->_html_select_array($rslist,$value["id"],1);
		}
		return $rslist;
	}

	function _html_select_array(&$rslist,$parentid=0,$space_id=1)
	{
		if(!$this->catelist)
		{
			return false;
		}
		$space = "";
		for($i=0;$i<$space_id;$i++)
		{
			$space .= "　　";
		}
		foreach($this->catelist AS $key=>$value)
		{
			if($value["parentid"] == $parentid)
			{
				$value["space"] = $space;
				$value["isend"] = true;
				if($this->parentlist && in_array($value["id"],$this->parentlist))
				{
					$value["isend"] = false;
				}
				$rslist[] = $value;
				$this->_html_select_array($rslist,$value["id"],($space_id+1));
			}
		}
		return true;
	}


	//放在List操作中应用到的html表单
	function html_select($select_id="cateid",$selected=0,$lang="",$stop_pid=0)
	{
		$select = "<select name='".$select_id."' id='".$select_id."'>";
		if($lang)
		{
			$select.= "<option value='0'>".$lang."</option>";
		}
		if($this->rootlist && is_array($this->rootlist) && count($this->rootlist)>0)
		{
			foreach($this->rootlist AS $key=>$value)
			{
				$select .= "<option value='".$value["id"]."'";
				if($selected == $value["id"])
				{
					$select.= " selected";
				}
				$select .= ">".$space.$value["cate_name"];
				if(!$value["status"])
				{
					$select .= "【已停用】";
				}
				if($value["linkurl"])
				{
					$select .= "【已使用外链】";
				}
				$select .= "</option>";
				$this->html_ext = "";
				$this->_html_select($value["id"],$selected,1,$stop_pid);
				$select.= $this->html_ext;
			}
		}
		$select .= "</select>";
		return $select;
	}

	//根据当前分类取得子类HTML
	function _html_select($parentid=0,$selected=0,$space_id=1,$stop_pid=0)
	{
		if(!$this->catelist)
		{
			return false;
		}
		$space = "";
		for($i=0;$i<$space_id;$i++)
		{
			$space .= "&nbsp; &nbsp; ";
		}
		foreach($this->catelist AS $key=>$value)
		{
			if($value["parentid"] == $parentid)
			{
				$this->html_ext .= "<option value='".$value["id"]."'";
				if($value["id"] == $selected)
				{
					$this->html_ext .= " selected";
				}
				$this->html_ext .= ">".$space.$value["cate_name"];
				if(!$value["status"])
				{
					$this->html_ext .= "【已停用】";
				}
				if($value["linkurl"])
				{
					$this->html_ext .= "【已使用外链】";
				}
				$this->html_ext .= "</option>";
				if($parentid != $stop_pid || !$stop_pid)
				{
					$this->_html_select($value["id"],$selected,($space_id+1),$stop_pid);
				}
			}
		}
		return true;
	}	
	
	
		
}